package com.genesis.mall.service.impl;

import com.genesis.mall.dto.CouponHistoryDetailDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PageInfo;
import com.genesis.mall.entity.SmsCouponHistory;
import com.genesis.mall.mapper.SmsCouponHistoryMapper;
import com.genesis.mall.mapper.SmsCouponMapper;
import com.genesis.mall.service.ISmsCouponHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 优惠券使用、领取历史表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class SmsCouponHistoryServiceImpl extends ServiceImpl<SmsCouponHistoryMapper, SmsCouponHistory> implements ISmsCouponHistoryService {
    @Autowired
    private SmsCouponHistoryMapper couponHistoryMapper;
    @Override
    public List<CouponHistoryDetailDto> findById(Long memberId) {
        return couponHistoryMapper.findById(memberId);
    }

    /**
     * 根据优惠券id，使用状态，订单编号分页获取领取记录
     * @param couponId
     * @param orderSn
     * @param pageNum
     * @param pageSize
     * @param useStatus
     * @return
     */
    @Override
    public PageInfo historyRecord(Integer couponId, String orderSn, Integer pageNum, Integer pageSize, Integer useStatus) {
        Page<SmsCouponHistory> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(couponId != null,SmsCouponHistory::getCouponId,couponId)
                .eq(useStatus != null, SmsCouponHistory::getUseStatus,useStatus)
                .eq(StringUtils.isNotEmpty(orderSn), SmsCouponHistory::getOrderSn,orderSn);

        Page<SmsCouponHistory> historyRecordPage = this.page(page, lqw);
        int total = (int) historyRecordPage.getTotal();
        List<SmsCouponHistory> historyList = historyRecordPage.getRecords();
        Integer totalPage = PageInfo.totalPage(pageSize, total);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(historyList);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(totalPage);

        return pageInfo;
    }
}
